import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;


class ReportController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def home = {}

    def list = {
        if(!params.max) params.max = 10
	def showReport
	def sProject
	switch (session.user.role.name) {
		case "Student": 
			showReport = Report.findAllWhere(student: session.user) 
			break
		case "Organiser":
		case "Supervisor":
			def project = Project.get(params.projectId)
			showReport = Report.findAllWhere(project:project)
			break
	}			
	return [ reportList : showReport ]	
    }


    def show = {
        def report = Report.get( params.id )

        if(!report) {
            flash.message = "Report not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ report : report ] }
    }

    def delete = {
        def report = Report.get( params.id )
        if(report) {
            report.delete()
            flash.message = "Report ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Report not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def report = Report.get( params.id )

        if(!report) {
            flash.message = "Report not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ report : report ]
        }
    }

    def update = {
        def report = Report.get( params.id )
        if(report) {
            report.properties = params
            if(!report.hasErrors() && report.save()) {
                flash.message = "Report ${params.id} updated"
                redirect(action:show,id:report.id)
            }
            else {
                render(view:'edit',model:[report:report])
            }
        }
        else {
            flash.message = "Report not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def report = new Report()
        report.properties = params
        return ['report':report]
    }

    def save = {
        def report = new Report(params)
	report.student = session.user
	report.project = session.user.chosenProject() 
/*
	def allocation = Allocation.findByStudent(session.user)
	log.debug(allocation)
	report.project = allocation.finalAllocation	
*/
	  uploadFileData(report)
        if(!report.hasErrors() && report.save()) {
            flash.message = "Report ${report.id} created"
            redirect(action:show,id:report.id)
        }
        else {
            render(view:'create',model:[report:report])
        }
    }

    /**
     * This will get the file from the multipart request
     * and save it to the todo.
     */
    def uploadFileData = { report ->
        if (request instanceof MultipartHttpServletRequest) {
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
            CommonsMultipartFile file = (CommonsMultipartFile)multiRequest.getFile("associatedFile");
            report.fileName = file.originalFilename
            report.contentType = file.contentType
            report.associatedFile = file.bytes
        }
    }

    def downloadFile = {
        // look up the report 
        def report = Report.get( params.id )    

        // send the file to the output stream
        response.setHeader("Content-disposition", "attachment; filename=${report.fileName}")
        response.contentType = report.contentType
        response.outputStream << report.associatedFile
    }
	

}
